package controller

import (
	"gitee.com/zhoulvvv/my_go_gateway/dto"
	"gitee.com/zhoulvvv/my_go_gateway/middleware"
	"gitee.com/zhoulvvv/my_go_gateway/services"
	"github.com/gin-gonic/gin"
)

type ServiceController struct {
}

func RegisterServiceController(group *gin.RouterGroup) {
	service := &ServiceController{}
	group.GET("/service_list", service.ServiceList)
	group.GET("/service_delete", service.ServiceDelete)
	group.GET("/service_detail", service.ServiceDetail)
	group.GET("/service_stat", service.ServiceStat) // 服务流量统计
	group.POST("/service_add_http", service.ServiceAddHTTP)
	group.POST("/service_update_http", service.ServiceUpdateHTTP)
	group.POST("/service_add_tcp", service.ServiceAddTCP)
	group.POST("/service_update_tcp", service.ServiceUpdateTCP)
	group.POST("/service_add_grpc", service.ServiceAddGRPC)
	group.POST("/service_update_grpc", service.ServiceUpdateGRPC)
}

// ServiceStat godoc
// @Summary 服务流量统计
// @Description 服务流量统计
// @Tags 服务管理
// @ID /service/service_stat
// @Accept  json
// @Produce  json
// @Param id query int true "服务Id"
// @Success 200 {object} middleware.Response{data=dto.ServiceStatOutput} "success"
// @Router /service/service_stat [get]
func (s *ServiceController) ServiceStat(c *gin.Context) {
	params := &dto.ServiceStatInput{}
	if err := params.BindValidaParam(c); err != nil {
		middleware.ResponseError(c, 3000, err)
		return
	}
	serviceServices, err := services.NewServiceServices()
	if err != nil {
		middleware.ResponseError(c, 3000, err)
		return
	}
	serviceStat, err := serviceServices.GetServiceStat(c, params)
	if err != nil {
		middleware.ResponseError(c, 3000, err)
		return
	}
	middleware.ResponseSuccess(c, serviceStat)
}

// ServiceDetail godoc
// @Summary 获取服务详情
// @Description 获取服务详情
// @Tags 服务管理
// @ID /service/service_detail
// @Accept  json
// @Produce  json
// @Param id query int true "服务Id"
// @Success 200 {object} middleware.Response{data=dao.ServiceDetail} "success"
// @Router /service/service_detail [get]
func (s *ServiceController) ServiceDetail(c *gin.Context) {
	params := &dto.ServiceDetailInput{}
	if err := params.BindValidaParam(c); err != nil {
		middleware.ResponseError(c, 3000, err)
		return
	}
	serviceServices, err := services.NewServiceServices()
	if err != nil {
		middleware.ResponseError(c, 3000, err)
		return
	}
	serviceDetail, err := serviceServices.GetServiceDetail(c, params)
	if err != nil {
		middleware.ResponseError(c, 3000, err)
		return
	}
	middleware.ResponseSuccess(c, serviceDetail)
}

// ServiceUpdateHTTP godoc
// @Summary 服务修改HTTP
// @Description 服务修改HTTP
// @Tags 服务管理
// @ID /service/service_update_http
// @Accept  json
// @Produce  json
// @Param body body dto.ServiceUpdateHTTPInput true "body"
// @Success 200 {object} middleware.Response{data=string} "success"
// @Router /service/service_update_http [post]
func (s *ServiceController) ServiceUpdateHTTP(c *gin.Context) {
	params := &dto.ServiceUpdateHTTPInput{}
	if err := params.BindValidaParam(c); err != nil {
		middleware.ResponseError(c, 3000, err)
		return
	}
	serviceServices, err := services.NewServiceServices()
	if err != nil {
		middleware.ResponseError(c, 3000, err)
		return
	}
	err = serviceServices.UpdateServiceInfoHTTP(c, params)
	if err != nil {
		middleware.ResponseError(c, 3000, err)
		return
	}
	middleware.ResponseSuccess(c, "服务修改成功")
}

// ServiceUpdateGRPC godoc
// @Summary 服务修改GRPC
// @Description 服务修改GRPC
// @Tags 服务管理
// @ID /service/service_update_grpc
// @Accept  json
// @Produce  json
// @Param body body dto.ServiceUpdateGRPCInput true "body"
// @Success 200 {object} middleware.Response{data=string} "success"
// @Router /service/service_update_grpc [post]
func (s *ServiceController) ServiceUpdateGRPC(c *gin.Context) {
	params := &dto.ServiceUpdateGRPCInput{}
	if err := params.BindValidaParam(c); err != nil {
		middleware.ResponseError(c, 3000, err)
		return
	}
	serviceServices, err := services.NewServiceServices()
	if err != nil {
		middleware.ResponseError(c, 3000, err)
		return
	}
	err = serviceServices.UpdateServiceInfoGRPC(c, params)
	if err != nil {
		middleware.ResponseError(c, 3000, err)
		return
	}
	middleware.ResponseSuccess(c, "服务修改成功")
}

// ServiceUpdateTCP godoc
// @Summary 服务修改TCP
// @Description 服务修改TCP
// @Tags 服务管理
// @ID /service/service_update_tcp
// @Accept  json
// @Produce  json
// @Param body body dto.ServiceUpdateTCPInput true "body"
// @Success 200 {object} middleware.Response{data=string} "success"
// @Router /service/service_update_tcp [post]
func (s *ServiceController) ServiceUpdateTCP(c *gin.Context) {
	params := &dto.ServiceUpdateTCPInput{}
	if err := params.BindValidaParam(c); err != nil {
		middleware.ResponseError(c, 3000, err)
		return
	}
	serviceServices, err := services.NewServiceServices()
	if err != nil {
		middleware.ResponseError(c, 3000, err)
		return
	}
	err = serviceServices.UpdateServiceInfoTCP(c, params)
	if err != nil {
		middleware.ResponseError(c, 3000, err)
		return
	}
	middleware.ResponseSuccess(c, "服务修改成功")
}

// ServiceAddHTTP godoc
// @Summary 服务添加HTTP
// @Description 服务添加HTTP
// @Tags 服务管理
// @ID /service/service_add_http
// @Accept  json
// @Produce  json
// @Param body body dto.ServiceAddHTTPInput true "body"
// @Success 200 {object} middleware.Response{data=string} "success"
// @Router /service/service_add_http [post]
func (s *ServiceController) ServiceAddHTTP(c *gin.Context) {
	params := &dto.ServiceAddHTTPInput{}
	if err := params.BindValidaParam(c); err != nil {
		middleware.ResponseError(c, 3000, err)
		return
	}
	serviceServices, err := services.NewServiceServices()
	if err != nil {
		middleware.ResponseError(c, 3000, err)
		return
	}
	err = serviceServices.AddServiceInfoHTTP(c, params)
	if err != nil {
		middleware.ResponseError(c, 3000, err)
		return
	}
	middleware.ResponseSuccess(c, "服务添加成功")
}

// ServiceAddTCP godoc
// @Summary 服务添加TCP
// @Description 服务添加TCP
// @Tags 服务管理
// @ID /service/service_add_tcp
// @Accept  json
// @Produce  json
// @Param body body dto.ServiceAddTCPInput true "body"
// @Success 200 {object} middleware.Response{data=string} "success"
// @Router /service/service_add_tcp [post]
func (s *ServiceController) ServiceAddTCP(c *gin.Context) {
	params := &dto.ServiceAddTCPInput{}
	if err := params.BindValidaParam(c); err != nil {
		middleware.ResponseError(c, 3000, err)
		return
	}
	serviceServices, err := services.NewServiceServices()
	if err != nil {
		middleware.ResponseError(c, 3000, err)
		return
	}
	err = serviceServices.AddServiceInfoTCP(c, params)
	if err != nil {
		middleware.ResponseError(c, 3000, err)
		return
	}
	middleware.ResponseSuccess(c, "服务添加成功")
}

// ServiceAddGRPC godoc
// @Summary 服务添加GRPC
// @Description 服务添加GRPC
// @Tags 服务管理
// @ID /service/service_add_grpc
// @Accept  json
// @Produce  json
// @Param body body dto.ServiceAddGRPCInput true "body"
// @Success 200 {object} middleware.Response{data=string} "success"
// @Router /service/service_add_grpc [post]
func (s *ServiceController) ServiceAddGRPC(c *gin.Context) {
	params := &dto.ServiceAddGRPCInput{}
	if err := params.BindValidaParam(c); err != nil {
		middleware.ResponseError(c, 3000, err)
		return
	}
	serviceServices, err := services.NewServiceServices()
	if err != nil {
		middleware.ResponseError(c, 3000, err)
		return
	}
	err = serviceServices.AddServiceInfoGRPC(c, params)
	if err != nil {
		middleware.ResponseError(c, 3000, err)
		return
	}
	middleware.ResponseSuccess(c, "服务添加成功")
}

// ServiceList godoc
// @Summary 获取服务列表
// @Description 获取服务列表
// @Tags 服务管理
// @ID /service/service_list
// @Accept  json
// @Produce  json
// @Param info query string false "关键词"
// @Param page_no query int true "页数"
// @Param page_size query int true "每页数量"
// @Success 200 {object} middleware.Response{data=dto.ServiceListOutput} "success"
// @Router /service/service_list [get]
func (s *ServiceController) ServiceList(c *gin.Context) {
	params := &dto.ServiceListInput{}
	if err := params.BindValidaParam(c); err != nil {
		middleware.ResponseError(c, 3000, err)
		return
	}
	serviceServices, err := services.NewServiceServices()
	if err != nil {
		middleware.ResponseError(c, 3000, err)
		return
	}
	// 查询服务列表
	out, err := serviceServices.GetPageList(c, params)
	if err != nil {
		middleware.ResponseError(c, 3000, err)
		return
	}
	middleware.ResponseSuccess(c, out)
}

// ServiceDelete godoc
// @Summary 服务删除
// @Description 服务删除
// @Tags 服务管理
// @ID /service/service_delete
// @Accept  json
// @Produce  json
// @Param id query int true "服务Id"
// @Success 200 {object} middleware.Response{data=string} "success"
// @Router /service/service_delete [get]
func (s *ServiceController) ServiceDelete(c *gin.Context) {
	params := &dto.ServiceDeleteInput{}
	if err := params.BindValidaParam(c); err != nil {
		middleware.ResponseError(c, 3000, err)
		return
	}
	serviceServices, err := services.NewServiceServices()
	if err != nil {
		middleware.ResponseError(c, 3000, err)
		return
	}
	err = serviceServices.DeleteServiceInfo(c, params)
	if err != nil {
		middleware.ResponseError(c, 3000, err)
		return
	}
	middleware.ResponseSuccess(c, "服务删除成功")
}
